<?php

namespace common\model\sell66;

use common\db\Sell66;
use function MongoDB\BSON\fromJSON;

class UserLoginLog extends Sell66
{

    protected $table = 'user_login_log';

    protected $pk = 'user_bank_id';

    protected $dateFormat = 'Y-m-d H:i:s';

    protected $autoWriteTimestamp = 'datetime';

    protected $createTime = 'login_at';

    protected $updateTime = 'updated_at';

    public static function createUserLoginLog($addData)
    {

        $userLoginLog = new self ();

        if (!empty ($addData['user_id'])) {
            $userLoginLog->user_id = $addData['user_id'];
        }

        if (!empty ($addData['personaname'])) {
            $userLoginLog->personaname = $addData['personaname'];
        }

        if (!empty ($addData['login_ip'])) {
            $userLoginLog->login_ip = $addData['login_ip'];
        }

        $userLoginLog->save();
    }

    /**
     * 检查用户昵称是否含有sell66
     * @param $nikeName
     * @return bool
     */
    public static function checkNickName($nikeName)
    {
        if (strpos(strtolower($nikeName), 'sell66.com') !== false) {
            return true;
        }
        return false;
    }

    /**
     * 获取活动中用户第一次登陆时间
     * @param array $timeScope
     * @param $userId
     * @return bool
     */
    public static function getActUserLoginTime(array $timeScope, $userId)
    {
        $between = array(
            $timeScope['start_time'],
            $timeScope['end_time'],
        );
        $res = self::whereBetween('login_at', $between)->where('user_id', '=', $userId)->order('login_at', 'asc')->select();

        foreach ($res as $val){
            if(self::checkNickName($val['personaname'])){
               return $val['login_at'];
            }
        }
        return false;
    }
}